package com.lry.basic.proxy.demo1;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TransactionalInvocationHandler implements CustomInvocationHandler {
    Object target;
    public TransactionalInvocationHandler(Object target){
        this.target=target;
    }

    @Override
    public Object invoke(Method method,Object... args) {

        try {
            if(method.isAnnotationPresent(Tran.class)){
                System.out.println("begin transactional");
            }

            Object obj = method.invoke(target,args);

            if(method.isAnnotationPresent(Tran.class)){
                System.out.println("commit transactional");
            }

            return obj;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
